package com.cjzuji.rental;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@SpringBootTest
public class ExcelRead {

    String PATH = "E:\\123.xlsx";
    @Test
    public void  testRead() throws Exception {
        //获取文件流
        FileInputStream fileInputStream = new FileInputStream(PATH);
        //床架一个工作表
        // 根据文件扩展名或内容判断是 .xls 还是 .xlsx
        Workbook workbook = null;
        if (PATH.endsWith(".xlsx")) {
            workbook = new XSSFWorkbook(fileInputStream); // 处理 .xlsx 文件
        } else if (PATH.endsWith(".xls")) {
            workbook = new HSSFWorkbook(fileInputStream); // 处理 .xls 文件
        } else {
            throw new IllegalArgumentException("The specified file is not Excel format");
        }

        //获取 sheet 第0个
        Sheet sheet = workbook.getSheetAt(0);

        //获取行
        Row row = sheet.getRow(0);
        //获取列
        Cell cell = row.getCell(0);

        String stringCellValue = cell.getStringCellValue();
        System.out.println(stringCellValue);
        fileInputStream.close();
    }
    }
